// @ts-check
import XYZ from 'ol/source/XYZ'
import { OSM, BingMaps } from 'ol/source'
import { WMTS } from 'ol/source'
import WMTSTileGrid from 'ol/tilegrid/WMTS'
import { get as getProjection } from 'ol/proj'
import { getWidth, getTopLeft } from 'ol/extent'
import baidu from './baiduLayer'
import TileCache from 'ol/TileCache'

const TIANDI_KEY = 'b117c76dd844ff21b36b186a911157a6' //zwb's

// 对应的值是固定的
const layerTypeMap = {
  SL: ['vec', 'cva'], // [矢量底图, 矢量注记]
  YX: ['img', 'cia'], // [影像底图, 影像注记]
  DX: ['ter', 'cta'] // [地形晕渲, 地形注记]
}
// const projection = get('EPSG:4326')
// const projectionExtent = projection?.getExtent()
// const size = getWidth(projectionExtent!) / 256
// const resolutions = [] as any
// for (let z = 2; z < 19; ++z) {
//   resolutions[z] = size / Math.pow(2, z)
// }
const layerType = 'SL' // 加载矢量底图
// const layerType = 'YX'// 加载影像底图
// const layerType = 'DX' // 加载地形晕渲
// c: 经纬度投影 w: 球面墨卡托投影
const matrixSet = 'c'

// const projection = getProjection('EPSG:900913')
const projection = getProjection('EPSG:4326')
const projectionExtent = projection.getExtent()
const size = getWidth(projectionExtent) / 256
const resolutions = new Array(19)
const matrixIds = new Array(19)
for (let z = 0; z < 19; ++z) {
  resolutions[z] = size / Math.pow(2, z)
  matrixIds[z] = z
}
const tileGrid = new WMTSTileGrid({
  origin: getTopLeft(projectionExtent),
  resolutions: resolutions,
  matrixIds: matrixIds
})

export default [
  {
    name: '高德地图',
    value: [
      new XYZ({
        // url: 'http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}'
        url: 'http://webst0{1-4}.is.autonavi.com/appmaptile?style=7&x={x}&y={y}&z={z}&lang=zh_cn&scale=1&size=1',
        // url: 'http://webst0{1-4}.is.autonavi.com/appmaptile?style=1&x={x}&y={y}&z={z}&lang=zh_cn&scale=1&size=1'
        cacheSize: 1000
      })
    ],
    value2: [
      new XYZ({
        // 6 卫星
        url: 'http://webst0{1-4}.is.autonavi.com/appmaptile?style=6&x={x}&y={y}&z={z}&lang=zh_cn&scale=1&size=1'
      })
    ],
    id: '0'
  },
  {
    name: 'OSM地图',
    value: [new OSM()],
    id: '1'
  },
  {
    name: '必应地图',
    value: [
      new BingMaps({
        key: 'Ag06tvwdiSEW4qkUHbnLI0waVaSN-jzs9oX8FQh8GqAMhWaC8RLjWeZx0ww_Y7Or',
        imagerySet: 'RoadOnDemand'
      })
    ],
    id: '2'
  },
  {
    name: '百度地图',
    value: [baidu],
    id: '3'
  },
  {
    name: 'Google地图',
    value: [
      new XYZ({
        // url:'http://www.google.cn/maps/vt?lyrs=t@189&gl=cn&x={x}&y={y}&z={z}'
        // url: 'https://gps.teredy.com/ol_google/maps/vt/pb=!1m4!1m3!1i{z}!2i{x}!3i{y}!2m3!1e0!2sm!3i380072576!3m8!2szh-CN!3scn!5e1105!12m4!1e68!2m2!1sset!2sRoadmap!4e0!5m1!1e0'
        //谷歌卫星地图         url:'http://mt0.google.cn/maps/vt?lyrs=s@773&gl=cn&x={x}&y={y}&z={z}'
        //url:'http://www.google.cn/maps/vt?lyrs=m@189&gl=cn&x={x}&y={y}&z={z}'
        //url:'http://www.google.cn/maps/vt?lyrs=s@189&gl=cn&x={x}&y={y}&z={z}'
        //url:'http://www.google.cn/maps/vt?lyrs=h@189&gl=cn&x={x}&y={y}&z={z}'
        url: 'https://mt{s}.google.cn/vt/lyrs=m&x={x}&y={y}&z={z}',
        crossOrigin: 'anonymous'
      })
    ],
    value2: [
      new XYZ({
        // url:'http://www.google.cn/maps/vt?lyrs=t@189&gl=cn&x={x}&y={y}&z={z}'
        // url: 'https://gps.teredy.com/ol_google/maps/vt/pb=!1m4!1m3!1i{z}!2i{x}!3i{y}!2m3!1e0!2sm!3i380072576!3m8!2szh-CN!3scn!5e1105!12m4!1e68!2m2!1sset!2sRoadmap!4e0!5m1!1e0'
        //谷歌卫星地图
        url: 'http://mt0.google.cn/maps/vt?lyrs=s@773&gl=cn&x={x}&y={y}&z={z}'
        //url:'http://www.google.cn/maps/vt?lyrs=m@189&gl=cn&x={x}&y={y}&z={z}'
        //url:'http://www.google.cn/maps/vt?lyrs=s@189&gl=cn&x={x}&y={y}&z={z}'
        //url:'http://www.google.cn/maps/vt?lyrs=h@189&gl=cn&x={x}&y={y}&z={z}'
      })
    ],
    id: '4'
  },
  {
    name: '天地图',
    value: [
      new XYZ({
        url: 'https://gps.teredy.com/ol_tiandi/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=49ea1deec0ffd88ef13a3f69987e9a63'
      }),
      new XYZ({
        url: 'https://gps.teredy.com/ol_tiandi/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=49ea1deec0ffd88ef13a3f69987e9a63' //标注图层
      })
    ],
    value2: [
      new XYZ({
        url: 'https://gps.teredy.com/ol_tiandi/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=49ea1deec0ffd88ef13a3f69987e9a63'
      }),
      new XYZ({
        url: 'https://gps.teredy.com/ol_tiandi/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=49ea1deec0ffd88ef13a3f69987e9a63'
      }),
      new XYZ({
        url: 'https://gps.teredy.com/ol_tiandi/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=49ea1deec0ffd88ef13a3f69987e9a63' //标注图层
      })
    ],
    id: '5'
  },
  {
    name: '天地图2',
    value: [
      // 底图
      new WMTS({
        url: `http://t{0-6}.tianditu.com/${layerTypeMap[layerType][0]}_${matrixSet}/wmts?tk=${TIANDI_KEY}`,
        layer: layerTypeMap[layerType][0],
        matrixSet: matrixSet,
        style: 'default',
        crossOrigin: 'anonymous', // 解决跨域问题 如无该需求可不添加
        format: 'tiles',
        wrapX: true,
        tileGrid: tileGrid,
        requestEncoding: 'KVP',
        attributions: ['&copy; <a target="_blank" href="http://www.tianditu.gov.cn/">天地图</a>'],
        tilePixelRatio: 1
      }),
      // 标注
      new WMTS({
        url: `http://t{0-6}.tianditu.com/${layerTypeMap[layerType][1]}_${matrixSet}/wmts?tk=${TIANDI_KEY}`,
        layer: layerTypeMap[layerType][1],
        matrixSet: matrixSet,
        style: 'default',
        crossOrigin: 'anonymous', // 解决跨域问题 如无该需求可不添加
        format: 'tiles',
        wrapX: true,
        tileGrid: tileGrid
      })
    ],
    value2: [
      new XYZ({
        url: 'https://gps.teredy.com/ol_tiandi/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=49ea1deec0ffd88ef13a3f69987e9a63'
      }),
      new XYZ({
        url: 'https://gps.teredy.com/ol_tiandi/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=49ea1deec0ffd88ef13a3f69987e9a63'
      }),
      new XYZ({
        url: 'https://gps.teredy.com/ol_tiandi/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=49ea1deec0ffd88ef13a3f69987e9a63' //标注图层
      })
    ],
    id: '6'
  },
  {
    name: '百度2',
    value: [
      new XYZ({
        url: 'http://online{0-3}.map.bdimg.com/onlinelabel/?qt=tile&x={x}&y={y}&z={z}&styles=pl&udt=20141103&scaler=1',
        crossOrigin: 'anonymous'
      })
    ],
    id: '7'
  },
  {
    name: 'ArcGIS',
    value: [
      new XYZ({
        url: 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}',
        crossOrigin: 'anonymous'
      })
    ],
    id: '8'
  }
]
